package com.shop.dao.implement;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.shop.dao.HibernateBaseDao;
import com.shop.dao.IOrderDao;
import com.shop.pojo.Order;

public class OrderDao extends HibernateBaseDao<Order> implements IOrderDao {
	@SuppressWarnings("unchecked")
	public List<Order> find(int firstResult, int maxResult) {
		String hql="from Order o order by o.time desc";
		
		return findByHql(hql,null, firstResult, maxResult,null);
	}
	public int count(){
		String hql="select count(*) from Order";
		return ((Long)find(hql, null)).intValue();
	}
	
	public List<Order> find(String userid, int firstResult, int maxResult) {
		String hql="from Order o where o.userid=:userid order by o.time desc";
		return findByHql(hql, "userid", userid, firstResult, maxResult,null);
	}
	@SuppressWarnings("unchecked")
	public int count(String userid) {
		String hql="select count(*) from Order o where o.userid=:userid";
		Map map = new HashMap();
		map.put("userid", userid);
		return ((Long)find(hql,map)).intValue();
	}
	
	public List<Order> find(Order.OrderStatus orderStatus, int firstResult, int maxResult){
		String hql="from Order o where o.status=:orderStatus order by time desc";
		return findByHql(hql, "orderStatus", orderStatus, firstResult, maxResult, null);
	}
	
	@SuppressWarnings("unchecked")
	public int count(Order.OrderStatus orderStatus){
		String hql="select count(*) from Order o where o.status=:orderStatus ";
		Map map=new HashMap();
		map.put("orderStatus", orderStatus);
		return ((Long)find(hql, map)).intValue();
	}
	@SuppressWarnings("unchecked")
	public List<Order> find(String orderid , String userid, Date startTime,Date endTime, int firstResult, int maxResult){
		
		boolean where=false;
		Map map = new  HashMap();
		
		StringBuilder hql=new StringBuilder("from Order o ");
		if(orderid!=null){
			hql.append("where o.id like :orderid ");
			map.put("orderid", "%"+orderid+"%");
			where=true;
		}
		if(userid!=null){
			if(where)hql.append("and o.userid=:userid ");
			else {
				hql.append("where o.userid=:userid ");
				where=true;
			}
			map.put("userid", userid);
		}
		if(startTime!=null){
			if(where)hql.append("and o.time>=:startTime ");
			else {
				hql.append("where o.time>=:startTime ");
				where=true;
			}
			map.put("startTime", startTime);
		}
		if(endTime!=null){
			if(where)hql.append("and o.time<=:endTime ");
			else {
				hql.append("where o.time<=:endTime ");
				where=true;
			}
			map.put("endTime", endTime);
		}
		hql.append("order by o.time desc");
		return findByHql(hql.toString(), map, firstResult, maxResult, null);
		
	}
	@SuppressWarnings("unchecked")
	public int count(String orderid , String userid, Date startTime,Date endTime){
		boolean where=false;
		Map map = new  HashMap();
		
		StringBuilder hql=new StringBuilder("select count(*) from Order o ");
		if(orderid!=null){
			hql.append("where o.id like :orderid ");
			map.put("orderid", "%"+orderid+"%");
			where=true;
		}
		if(userid!=null){
			if(where)hql.append("and o.userid=:userid ");
			else {
				hql.append("where o.userid=:userid ");
				where=true;
			}
			map.put("userid", userid);
		}
		if(startTime!=null){
			if(where)hql.append("and o.time>=:startTime ");
			else {
				hql.append("where o.time>=:startTime ");
				where=true;
			}
			map.put("startTime", startTime);
		}
		if(endTime!=null){
			if(where)hql.append("and o.time<=:endTime ");
			else {
				hql.append("where o.time<=:endTime ");
				where=true;
			}
			map.put("endTime", endTime);
		}
		return ((Long)find(hql.toString(), map)).intValue();
	}



}
